package cn.org.atool.generator.database.model;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.TypeHandler;

/**
 * 预定义好的字段
 *
 * @author wudarui
 */
@SuppressWarnings({"rawtypes", "unused", "UnusedReturnValue"})
@Getter
@Setter
@Accessors(chain = true)
public class CustomizedColumn {
    @Setter(AccessLevel.NONE)
    private String columnName;

    private String fieldName;

    private TypeName javaType;

    /**
     * 显式设置字段对应的java类型
     *
     * @param javaType 字段java类型
     * @param argTypes 如果是泛型, 知道的泛型参数类型
     * @return CustomizedColumn
     */
    public CustomizedColumn setJavaType(Class javaType, Class... argTypes) {
        if (argTypes == null || argTypes.length == 0) {
            this.javaType = ClassName.get(javaType);
        } else {
            this.javaType = ParameterizedTypeName.get(javaType, argTypes);
        }
        return this;
    }

    /**
     * typeHandler
     */
    private Class<? extends TypeHandler> typeHandler;
    /**
     * 默认不是大字段
     */
    @Setter(AccessLevel.NONE)
    private boolean notLarge = true;
    /**
     * 不生成映射字段
     */
    @Setter(AccessLevel.NONE)
    private boolean exclude = false;
    /**
     * update默认值
     */
    private String update;
    /**
     * insert默认值
     */
    private String insert;

    public CustomizedColumn(String columnName, String fieldName, Class javaType, Class... argTypes) {
        this.columnName = columnName;
        this.fieldName = fieldName;
        this.setJavaType(javaType, argTypes);
    }

    public CustomizedColumn(String columnName) {
        this.columnName = columnName;
    }

    /**
     * 设置为大字段
     *
     * @return ignore
     */
    public CustomizedColumn setLarge() {
        this.notLarge = false;
        return this;
    }

    /**
     * 设置为排除字段
     *
     * @return ignore
     */
    public CustomizedColumn setExclude() {
        this.exclude = true;
        return this;
    }
}